package com.wuwei.elearning.question.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwei.elearning.dto.ConversionMessage;
import com.wuwei.elearning.enumPackage.Constant;
import com.wuwei.elearning.messageQueen.Message;
import com.wuwei.elearning.messageQueen.MessageQueue;
import com.wuwei.elearning.question.domain.Ppt;
import com.wuwei.elearning.question.domain.Subject;
import com.wuwei.elearning.question.mapper.PptMapper;
import com.wuwei.elearning.question.mapper.SubjectMapper;
import com.wuwei.elearning.question.service.PptService;
import com.wuwei.elearning.service.CacheService;
import com.wuwei.elearning.utils.MinioUtil;
import com.wuwei.elearning.utils.TimeUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class pptServiceImpl extends ServiceImpl<PptMapper, Ppt> implements PptService {

    @Resource
    private PptMapper pptMapper;

    @Resource
    private MinioUtil minioUtil;
    @Autowired
    private TimeUtils timeUtils;

    @SneakyThrows
    @Override
    @Transactional
    public void insert(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        Ppt pptPojo = new Ppt();
        pptPojo.setName(originalFilename);
        pptMapper.insert(pptPojo);
//        asyncService.processPPT2PDFAsync(fileName,file,pptPojo.getId());
//        System.out.println("插入操作完成!"+ System.currentTimeMillis());
//        conversionProducer.sendConversionTask(KafkaConfig.PPT_CONVERSION_TOPIC, originalFilename,file, pptPojo.getId());

        // 1. 先将文件上传到MinIO
        String tempPath = "temp/" + originalFilename;
        minioUtil.uploadToPath(file,tempPath);

        // 2. 只发送元数据到Kafka
        Message message = new Message();
        message.setFileName(originalFilename);
        message.setId(pptPojo.getId());
        message.setMinioPath(tempPath); // 存储MinIO中的路径，而不是文件内容
        messageQueue.send(Constant.PPT_CONVERSION_TOPIC, message,message.getId());
    }

    @Autowired
    private MessageQueue messageQueue;

    /**
     * 删除数据库
     * 删除minio
     * 删除缓存
     * @param ppt
     */
    @Override
    @Transactional
    public void myDelete(Ppt ppt) {
        log.info("即将删除的ppt-{}",ppt.getName());
        Ppt selectById = pptMapper.selectById(ppt.getId());
        String objectName = selectById.getUrl();
        minioUtil.remove(objectName);
        String url = pptMapper.selectById(ppt.getId()).getUrl();
        // 构建Redis键
        String redisKey = Constant.MINIO_PDF + url;
        cacheService.delete(redisKey);
        pptMapper.deleteById(ppt.getId());
        List<Subject> subjects = subjectMapper.findAllLeafs();
        for (Subject subject : subjects) {
            String ppts = subject.getPpts();
            if (ppts!=null) {
                List<Long> pptList = JSON.parseArray(ppts, Long.class).stream().filter(id->!id.equals(ppt.getId())).collect(Collectors.toList());
                subject.setPpts(JSON.toJSONString(pptList));
                subjectMapper.updateById(subject);
            }
        }
    }

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private CacheService cacheService;


}
